package dao;

import java.util.List;

import models.Area;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class AreaDAO {
	
	public static Area findById(int id){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr = null;
		Area a = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM Area a WHERE a.id= :id");
			q.setParameter("id", id);
			a = (Area)q.uniqueResult();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return a;
	}
	
	@SuppressWarnings("unchecked")
	public static List<Area> findAll(){
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tr =null;
		List<Area> a = null;
		try{
			tr = session.beginTransaction();
			Query q = session.createQuery("FROM Area ORDER BY id");
			a = (List<Area>) q.list();
			tr.commit();
		}catch(HibernateException e){
			tr.rollback();
			e.printStackTrace();
		}finally{
			session.close();
		}
		return a;
	}
}
